import tkinter as tk
import subprocess
import threading

class ConsoleOutputWindow:
    def __init__(self, root, font_size=12, max_lines=3):
        self.root = root
        self.root.title("翻译")
        
        self.root.attributes('-topmost', True)
        
        self.font_size = font_size
        self.max_lines = max_lines
        
        self.canvas = tk.Canvas(root, bg="white")
        self.canvas.pack(expand=True, fill='both')
        
        self.lines = []
        self.process = None
        self.start_update_thread()

    def start_update_thread(self):
        self.process = subprocess.Popen(['python', 'whisper_test.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', universal_newlines=True)
        threading.Thread(target=self.update_text, daemon=True).start()

    def update_text(self):
        while True:
            output = self.process.stdout.readline()
            if output == '' and self.process.poll() is not None:
                break
            if output:
                self.lines.append(output.strip())
                if len(self.lines) > self.max_lines:
                    self.lines.pop(0)
                
                self.canvas.delete("all")
                
                y_offset = 20
                for line in self.lines:
                    if line.startswith(">>"):
                        color = "black"
                        line = line[2:].strip()
                    else:
                        color = "blue"
                    
                    self.canvas.create_text(10, y_offset, text=line, anchor="w", font=('黑体', self.font_size), fill=color)
                    y_offset += 30
        self.process.stdout.close()
        self.process.wait()

if __name__ == "__main__":
    font_size = 24
    max_lines = 12

    root = tk.Tk()
    root.geometry("1600x400")
    
    app = ConsoleOutputWindow(root, font_size=font_size, max_lines=max_lines)
    root.mainloop()